
$(function () {
    var goods_order_id = parseInt(GetQueryString('goods_order_id'));
    var html_data = {
        baseConfig: baseConfig,
        sessionid: '',
        sessionid_q: '',
        address_info:{},
        province_list:[],
        city_list:[],
        start_list:[{type:1, index:0},{type:1, index:1},{type:1, index:2},{type:1, index:3},{type:1, index:4}],
        goods_order_item: []

    };
    var app = new Vue({
        el: '#address_modefy',
        data: html_data,
        methods: {
            price_filter: price_filter,
            go_goods_detail:function (goods_id) {
                location.href = 'goods.html?goods_id='+goods_id
            },
            get_fengmian:function(img_url1, img_url2){
                if(img_url1){
                    return baseConfig.qn_host + img_url1+'?imageView2/1/w/270/h/270';
                }else if(img_url2){
                    return baseConfig.qn_host + img_url2+'?imageView2/1/w/270/h/270';
                }
            },
            del_file_img:function(img, goodsitem){
                Magiclamp.mcConfirm("提示", "确认删除图片么？", null, function () {
                    api.lgc.del_img_comment({goods_order_id: goodsitem.goods_order_id, goods_id:goodsitem.goods_id, img_file_id:img.id}).then(function (value) {
                        try{
                            goodsitem.comment_files_json = JSON.parse(value.result.files_json);
                        }catch (e) {
                            goodsitem.comment_files_json = [];
                        }

                    });
                });
            },
            del_video_img:function(goodsitem){
                Magiclamp.mcConfirm("提示", "确认删除封面么？", null, function () {
                    api.lgc.del_video_img_comment({goods_order_id: goodsitem.goods_order_id, goods_id:goodsitem.goods_id}).then(function (value) {
                        goodsitem.comment_videos_img__fileurl = "";

                    });
                });
            },
            upload_img: function (eleid, goodsitem) {
                var file = document.getElementById(eleid).files[0];
                var filename = file.name;
                var filetype = "unknown";
                if (filename) {
                    var index = filename.lastIndexOf(".");
                    if (index !== -1) {
                        filetype = filename.substring(index, filename.length);
                    }
                }else{
                    return
                }
                filetype = filetype.toLowerCase().replace(/\./, '');
                api.nf.get_upload_files_url({
                    filename: filename,
                    filetype: filetype,
                    access_type: "public"
                }).then(function (data) {
                    var result = data.result;
                    var fileid = result.fileid;
                    var posturl = result.posturl;
                    var params = result.params;

                    var putExtra = {
                        fname: filename,
                    };

                    var observable = qiniu.upload(file, result.params.key, result.params.token, putExtra, {});
                    var subscription = observable.subscribe(function (next) {

                    }, function (err) {
                    }, function (complete) {
                        // html_data.img_list.push({fileid: fileid, url: result.params.key});
                        api.lgc.add_img_comment({goods_order_id: goodsitem.goods_order_id, goods_id:goodsitem.goods_id, img_file_id:fileid}).then(function (value) {
                            try{
                                goodsitem.comment_files_json = JSON.parse(value.result.files_json);
                            }catch (e) {
                                goodsitem.comment_files_json = [];
                            }
                        })

                    }); // 这样传参形式也可以


                });

            },
            upload_video_img: function (eleid, goodsitem) {
                var file = document.getElementById(eleid).files[0];
                var filename = file.name;
                var filetype = "unknown";
                if (filename) {
                    var index = filename.lastIndexOf(".");
                    if (index !== -1) {
                        filetype = filename.substring(index, filename.length);
                    }
                }else{
                    return
                }
                filetype = filetype.toLowerCase().replace(/\./, '');
                api.nf.get_upload_files_url({
                    filename: filename,
                    filetype: filetype,
                    access_type: "public"
                }).then(function (data) {
                    var result = data.result;
                    var fileid = result.fileid;
                    var posturl = result.posturl;
                    var params = result.params;

                    var putExtra = {
                        fname: filename,
                    };

                    var observable = qiniu.upload(file, result.params.key, result.params.token, putExtra, {});
                    var subscription = observable.subscribe(function (next) {

                    }, function (err) {
                    }, function (complete) {
                        // html_data.img_list.push({fileid: fileid, url: result.params.key});
                        api.lgc.add_video_img_comment({goods_order_id: goodsitem.goods_order_id, goods_id:goodsitem.goods_id,
                            img_file_id:fileid}).then(function (value) {
                            try{
                                goodsitem.comment_videos_img__fileurl = value.result.video_img__fileurl;
                            }catch (e) {
                                goodsitem.comment_videos_img__fileurl = '';
                            }
                        })

                    }); // 这样传参形式也可以
                });

            },
            upload_video: function (eleid, goodsitem) {
                var file = document.getElementById(eleid).files[0];
                var filename = file.name;
                var filetype = "unknown";
                if (filename) {
                    var index = filename.lastIndexOf(".");
                    if (index !== -1) {
                        filetype = filename.substring(index, filename.length);
                    }
                }else{
                    return
                }
                filetype = filetype.toLowerCase().replace(/\./, '');
                api.nf.get_upload_files_url({
                    filename: filename,
                    filetype: filetype,
                    access_type: "public"
                }).then(function (data) {
                    var result = data.result;
                    var fileid = result.fileid;
                    var posturl = result.posturl;
                    var params = result.params;

                    var putExtra = {
                        fname: filename,
                    };

                    var observable = qiniu.upload(file, result.params.key, result.params.token, putExtra, {});
                    var subscription = observable.subscribe(function (next) {

                    }, function (err) {
                    }, function (complete) {
                        // html_data.img_list.push({fileid: fileid, url: result.params.key});
                        api.lgc.add_video_comment({goods_order_id: goodsitem.goods_order_id, goods_id:goodsitem.goods_id,
                            video_file_id:fileid}).then(function (value) {
                            try{
                                goodsitem.comment_videos_json = JSON.parse(value.result.videos_json);
                            }catch (e) {
                                goodsitem.comment_videos_json = [];
                            }
                        })

                    }); // 这样传参形式也可以
                });

            },
            dafen: function (item, index) {

                item.pingfen = index;
                for(var i=0;i<5;i++){
                    if(i<=index){
                        $("#star_"+item.id+"_"+i).attr("src", 'img2/star_big_light.png')
                    }else{
                        $("#star_"+item.id+"_"+i).attr("src", 'img2/star_big_normal.png')
                    }

                }

            },
            save_comment:function(){
                var is_ok = true;
                var data = [];
                _(html_data.goods_order_item).each(function (item) {

                    data.push({goods_id:item.goods_id, value:item.pingfen, content:item.pingjia});
                })
                if(!is_ok){
                    return;
                }

                api.lgc.create_multi_goods_order_comment({goods_order_id:goods_order_id, data:JSON.stringify(data)}).then(function (value) {
                    if(value.success){
                        Magiclamp.mcAlert("提示", "评价成功。", null, function () {

                        });
                    }
                })

            },
            del_video: function(goodsitem){
                Magiclamp.mcConfirm("提示", "确认删除视频么？", null, function () {
                    api.lgc.del_video_comment({goods_order_id: goodsitem.goods_order_id, goods_id:goodsitem.goods_id}).then(function (value) {
                        goodsitem.comment_videos_json = {};

                    });
                });
            },
            play_video: function (dt) {
                var v = document.getElementById("video" + dt.id);
                var vc = document.getElementById("video_play" + dt.id);
                if (!dt.playing) {

                    v.play();
                    dt.playing = true;
                    vc.style.display = "none";
                } else {
                    v.pause();
                    dt.playing = false;

                    vc.style.display = "block";
                }
            },
            page_back: function () {
                baseConfig.page_back();
            }
        }
    });

    html_data.sessionid = GetQueryString('sessionid');
    if (html_data.sessionid) {
        html_data.sessionid_q = "sessionid=" + html_data.sessionid;
        client_storage('sessionid', html_data.sessionid);
    }


    if(goods_order_id){
        api.lgc.get_goods_order_comment({goods_order_id: GetQueryString("goods_order_id")}).then(function (data) {

            html_data.orderinfo = data.result.goods_order;
            html_data.goods_order_item = data.result.gooods_order_item;
            _(html_data.goods_order_item).each(function (item) {
                try{
                    item.comment_files_json = JSON.parse(item.comment_files_json);
                }catch (e) {
                    item.comment_files_json = [];
                }
                try{
                    item.comment_videos_json = JSON.parse(item.comment_videos_json);
                }catch (e) {
                    item.comment_videos_json = {};
                }
                item.comment_videos_img__fileurl = item.comment_videos_img__fileurl;

            })
        });
    }


});
